package svc;

import utils.Utility;
import constants.Constants;
import dao.FileDao;
import dao.ResultDao;
import domain.BioRecord;
import domain.DelRecord;
import domain.InsRecord;
import domain.MkvRecord;
import domain.PolRecord;
import domain.RpoRecord;
import domain.result.Result;

public class ResultSvc {
	ResultDao dao = new ResultDao();
	FileDao fileDao = new FileDao();

	private boolean saveResult(int fileNo, MkvRecord mkv){
		boolean success = true;
		
		Result result = new Result();
		int year = 1;
		
		result.setCwt(mkv.getCwt_final());
		
		result.setEaa(mkv.getEaa_cyr()[year]);
		result.setMilkYield(mkv.getMilkyield_cyr()[year]);
		result.setMilkSales(mkv.getMilksales_cyr()[year]);
		result.setFeedCost(mkv.getFeedcost_cyr()[year]);
		result.setHeiferCost(mkv.getHeifercost_cyr()[year]);
		result.setCowSales(mkv.getCowsales_cyr()[year]);
		result.setCalfSales(mkv.getCalfsales_cyr()[year]);
		result.setLiveStockCost(mkv.getLivestockcost_cyr()[year]);
		result.setLaborCost(mkv.getLaborcost_cyr()[year]);
		result.setVetCost(mkv.getVetcost_cyr()[year]);
		result.setBreedingSupplyCost(mkv.getBreedingcost_cyr()[year]);
		result.setLoss(mkv.getLoss_cyr()[year]);
		result.setOtherCost(mkv.getOthercost_cyr()[year]);
		result.setFixedCost_Labor(mkv.getFixedcost_labor_cyr()[year]);
		result.setFixedCost_Other(mkv.getFixedcost_other_cyr()[year]);
		result.setBreedingCost(mkv.getBreedingcost_cyr()[year]);
		result.setFeedCostDry(mkv.getFeedcostdry_cyr()[year]);
		result.setFeedCostLac(mkv.getFeedcostlac_cyr()[year]);
		
		
		result.setHerdLife(mkv.getHerdlife()[year]);
		result.setCash(mkv.getCash_cyr()[year]);
		result.setClvInterval(mkv.getClvinterval()[0][year][0]);
		result.setDaysToConc(mkv.getDaystoconc()[0][0][year][0]);
		result.setCullRateAll(mkv.getCullrateall()[year]);
		result.setCullRateVol(mkv.getCullratevol()[year]);
		result.setCullRateInv(mkv.getCullrateinv()[year]);
		result.setPregnancyRate0(mkv.getPregnancyrate0()[0][year]);
		result.setPregnancyRate1(mkv.getPregnancyrate1()[0][year]);
		result.setDaysOpen(mkv.getDaysopen()[0][year]);
		result.setPregLaborCost(mkv.getPreglaborcost()[year]);
		result.setPregSupplyCost(mkv.getPregsupplycost()[year]);
		result.setPv_Open(mkv.getPv_open()[year]);
		result.setInseminationValue(mkv.getInseminationvalue()[0][0][year]);
		result.setOpenDays(mkv.getOpendays()[year]);
		result.setAbortRisk(mkv.getAbortrisk()[year]);
		result.setPv_Preg(mkv.getPv_preg()[year]);
		result.setCowValue(mkv.getCowvalue()[year]);
		result.setDimY(mkv.getDimy()[year]);
		result.setDfs(mkv.getDfs()[0][0][year][0]);
		result.setCullPriceY(mkv.getCullpricey()[year]);
		result.setReplOpen_Mlk(mkv.getRepl_open_mlk()[0][0][year]);
		result.setReplOpen_Cur(mkv.getReplopen_cur()[0][0][year]);
		result.setReplOpen_Rev(mkv.getReplopen_rev()[0][0][year]);
		result.setReplOpen_Iof(mkv.getReplopen_iof()[0][0][year]);
		result.setReplOpen_Rpo(mkv.getReplopen_rpo()[0][0][year]);
		result.setReplOpen_Dim(mkv.getReplopen_dim()[0][0][year]);
		result.setDvwp(mkv.getDvwp()[0][0][year][0]);
		result.setDlbm(mkv.getDlbm()[0][0][year][0]);
		result.setDaysOpen_Cost(mkv.getDaysopen_cost()[year]);
		result.setDaysPreg_Cost(mkv.getDayspreg_cost()[year]);
		
		success = dao.saveResult(fileNo, result);	
		
		return success;
	}
	
	private boolean savePolRpoDelResults(int fileNo, BioRecord bio, PolRecord pol, RpoRecord rpo, InsRecord ins, DelRecord del){
		boolean result = true;
		
		int p = dao.insertPolRecord(fileNo, pol);
		int r = dao.insertRpoRecord(fileNo, bio, rpo, ins);
		int d = dao.insertDelRecord(fileNo, del);
			
		if(p<0 || r<0 || d<0){
			result = false;
		}
		
		return result;
	}
	
	public boolean updateFileStatus(int fileNo, int newStatus) {
		return fileDao.updateFileStatus(fileNo, newStatus);
	}
	
	public boolean saveAllOutputs(int fileNo, BioRecord bio, MkvRecord mkv,
			PolRecord pol, RpoRecord rpo, InsRecord ins, DelRecord del) {
		
		boolean success = deleteAllOutputs(fileNo);
		if(success){
			boolean res1 = saveResult(fileNo, mkv);
			boolean res2 = savePolRpoDelResults(fileNo, bio, pol, rpo, ins, del);
			boolean stat = updateFileStatus(fileNo, Constants.ANALYZED);
			success = res1 && res2 && stat;
		}
		return success;
	}

	public Result getResult(int fileNo){
		Result res = dao.getResult(fileNo);
		
		res.setTotalRevenue(res.getMilkSales() + res.getCowSales() + res.getCalfSales());
		res.setTotalCosts(res.getFeedCost() + res.getBreedingSupplyCost() + res.getHeiferCost() +
				res.getVetCost() + res.getLoss() + res.getLaborCost() + res.getOtherCost() + 
				res.getFixedCost_Labor() + res.getFixedCost_Other());
		res.setTotalFixedCost(res.getFixedCost_Labor() + res.getFixedCost_Other());
		res.setRevenueMinusVarCost(res.getTotalRevenue() - (res.getTotalCosts() - res.getTotalFixedCost()));
		
		return res;
	}
	
	public boolean deleteAllOutputs(int fileNo){
		return dao.deleteAllOutputs(fileNo);
	}
	
	
	public String convertToPSPD(double value) {
		return Utility.roundToExactTwo(value/365);
	}
	
	public String convertToPCWT(double value, double cwt) {
		return Utility.roundToExactTwo(value/cwt);
	}
	
}
